<script lang="ts" setup>
import { useRoute } from 'vue-router';
import { http } from '@/utils/request';
import type { GoodsDetail } from '@/types/index';
import { ref, onMounted } from 'vue';
import goodCount from './components/good-count.vue';
import type { SkuEmit } from './components/sku.vue';
import Mysku from './components/sku.vue';
import goodDetail from './components/goodDetail.vue';
import goodHot from './components/goodHot.vue';
import useStore from '@/store';
import { message } from '@/components/XtxUI';
import type { CartItem } from '@/types/index';
const route = useRoute();
// 导入cart仓库
const cart = useStore().cart;
// 购买数量
const count = ref<number>(1);
const goods = ref<GoodsDetail>();
getGood(route.params.id as string);

// 当商品信息发生变化时，更新数据信息。
const skuId = ref('');
const attrsText = ref();

// 获得商品数据
async function getGood(id: string) {
  const res = await http<GoodsDetail>('get', '/goods', { id });

  goods.value = res.data.result;
  console.log('商品数据', goods.value);
}

// 自定义事件的回调
function changeSkuFn(skuInfo: SkuEmit) {
  console.log('当前选中的sku信息为：', skuInfo);
  skuId.value = skuInfo.skuId || '';
  // 购物车存储文本
  attrsText.value = skuInfo || '';
  // 非空判断
  if (goods.value && skuInfo.skuId) {
    // 更新商品最新价格，商品原价，商品库存
    goods.value.price = skuInfo.price;
    goods.value.oldPrice = skuInfo.oldPrice;
    goods.value.inventory = skuInfo.inventory;
  }
}

function addCart() {
  if (!attrsText.value) {
    return message({
      type: 'error',
      text: '请选择规格！',
    });
  }
  console.log('购物车选择消息', attrsText.value, count);
  // const data = { skuId: attrsText.value.skuId, count: count.value };

  // console.log(data);
  if (!goods.value) return;
  // 处理attrText
  const skuItem = goods.value.skus.find((item) => (item.id = skuId.value));
  console.log('skuId', skuItem);
  let attrsStr = '';
  skuItem?.specs.forEach((item) => {
    attrsStr += item.name + ': ' + item.valueName;
    attrsStr = attrsStr + '  ';
  });
  console.log('attrstext', attrsStr);

  const cartItem = {
    id: goods.value.id,
    name: goods.value?.name,
    picture: goods.value.mainPictures[0],
    price: goods.value.oldPrice,
    nowPrice: goods.value.price,
    stock: goods.value.inventory,
    count: count.value,
    skuId: skuId.value,
    attrsText: attrsStr,
    selected: true,
    isEffective: true,
  } as CartItem;
  console.log('购物车提交的消息', cartItem);
  cart.addCart(cartItem);
}
</script>
<template>
  <div class="xtx-goods-page">
    <div class="container">
      <!-- 商品信息 -->
      <div v-if="goods" class="goods-info">
        <div class="media">
          <!-- 图片预览区 -->
          <div class="goods-image">
            <!-- 图片预览组件 -->

            <!-- 通过图片预览组件实现图片预览效果 -->
            <XtxImageView :image-list="goods.mainPictures" />
          </div>
          <!-- 统计数量 -->
          <ul class="goods-sales">
            <li>
              <p>销量人气</p>
              <p>100+</p>
              <p><i class="iconfont icon-task-filling"></i>销量人气</p>
            </li>
            <li>
              <p>商品评价</p>
              <p>200+</p>
              <p><i class="iconfont icon-comment-filling"></i>查看评价</p>
            </li>
            <li>
              <p>收藏人气</p>
              <p>80+</p>
              <p><i class="iconfont icon-favorite-filling"></i>收藏商品</p>
            </li>
            <li>
              <p>品牌信息</p>
              <p>90+</p>
              <p><i class="iconfont icon-dynamic-filling"></i>品牌主页</p>
            </li>
          </ul>
        </div>
        <!-- 商品信息区 -->
        <div class="spec">
          <!-- 商品主要信息 -->
          <div class="goods-main">
            <p class="g-name">{{ goods.name }}}</p>
            <p class="g-desc">{{ goods.desc }}</p>
            <p class="g-desc">这是选中的商品规格</p>
            <p class="g-price">
              <span>{{ goods?.price }}</span>
              <span>{{ goods?.oldPrice }}</span>
            </p>
            <div class="g-service">
              <dl>
                <dt>促销</dt>
                <dd>12月好物放送，App领券购买直降120元</dd>
              </dl>
              <dl>
                <dt>配送</dt>
                <dd>至</dd>
                <dd>
                  <XtxCity />
                </dd>
              </dl>
              <dl>
                <dt>服务</dt>
                <dd>
                  <span>无忧退货</span>
                  <span>快速退款</span>
                  <span>免费包邮</span>
                  <a href="javascript:;">了解详情</a>
                </dd>
              </dl>
            </div>
          </div>
          <!-- 规格选择组件 -->
          <!-- <XtxsKu :goods="goods" @change="changeSku"></XtxsKu> -->
          <Mysku :goods="goods" @change="changeSkuFn"></Mysku>
          <!-- 数量选择组件 -->
          <goodCount v-model="count" :inventory="goods.inventory"></goodCount>

          <!-- 按钮组件 -->

          <MyButton
            style="margin: 10px 0 0 10px"
            size="large"
            type="info"
            @click="addCart"
            >加入购物车</MyButton
          >
        </div>
      </div>
      <div v-else class="goods-info xtx-loading"></div>
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品详情 -->

          <div class="goods-tabs" v-if="goods">
            <goodDetail :goods="goods"></goodDetail>
          </div>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
          <goodHot :type="1"></goodHot>
          <goodHot :type="2"></goodHot>
          <goodHot :type="3"></goodHot>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped lang="less">
.xtx-loading {
  background-color: #fff;
  background-image: url('@/assets/images/loading.gif');
  background-position: center;
  background-repeat: no-repeat;
}
.container {
  margin-top: 20px;
}

// 商品信息
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}

// 图片预览区
.goods-image {
  width: 480px;
  height: 400px;
  background-color: #eee;
}

// 统计数量
.goods-sales {
  display: flex;
  width: 400px;
  align-items: center;
  text-align: center;
  height: 140px;
  li {
    flex: 1;
    position: relative;
    ~ li::after {
      position: absolute;
      top: 10px;
      left: 0;
      height: 60px;
      border-left: 1px solid #e4e4e4;
      content: '';
    }
    p {
      &:first-child {
        color: #999;
      }
      &:nth-child(2) {
        color: @priceColor;
        margin-top: 10px;
      }
      &:last-child {
        color: #666;
        margin-top: 10px;
        i {
          color: @xtxColor;
          font-size: 14px;
          margin-right: 2px;
        }
        &:hover {
          color: @xtxColor;
          cursor: pointer;
        }
      }
    }
  }
}

// 商品信息区
.spec {
  .g-name {
    font-size: 22px;
  }
  .g-desc {
    color: #999;
    margin-top: 10px;
  }
  .g-price {
    margin-top: 10px;
    span {
      &::before {
        content: '¥';
        font-size: 14px;
      }
      &:first-child {
        color: @priceColor;
        margin-right: 10px;
        font-size: 22px;
      }
      &:last-child {
        color: #999;
        text-decoration: line-through;
        font-size: 16px;
      }
    }
  }
  .g-service {
    background: #f5f5f5;
    width: 500px;
    padding: 20px 10px 0 10px;
    margin-top: 10px;
    dl {
      padding-bottom: 20px;
      display: flex;
      align-items: center;
      dt {
        width: 50px;
        color: #999;
      }
      dd {
        color: #666;
        &:last-child {
          span {
            margin-right: 10px;
            &::before {
              content: '•';
              color: @xtxColor;
              margin-right: 2px;
            }
          }
          a {
            color: @xtxColor;
          }
        }
      }
    }
  }
}

.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>
